package com.base.home.act;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.view.WindowManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.ChannelBean;
import com.base.commonlib.bean.TabEntity;
import com.base.commonlib.event.TabEnent;
import com.base.commonlib.utils.RxBus2;
import com.base.home.R;
import com.base.home.databinding.ActivityTabmanagerBinding;
import com.base.home.presenter.TabManagerPresenter;
import com.base.home.view.TabManagerView;
import com.base.home.wedit.ChannelAdapter;
import com.base.home.wedit.GridSpacingItemDecoration;
import com.base.home.wedit.ItemDragCallback;

import java.util.ArrayList;
import java.util.List;

import static com.base.commonlib.utils.DisplayUtil.dip2px;

/**
 * Created by weikailiang on 2020/2/17.
 */
@Route(path = "/home/tabmanager")
public class TabManagerActivity extends CBaseBarMvpActivity<TabManagerPresenter>  implements TabManagerView,ChannelAdapter.onItemRangeChangeListener{

    private ActivityTabmanagerBinding mBinding;

    private List<ChannelBean> mList;
    private ChannelAdapter mAdapter;
    @Override
    protected int getLayoutResource() {
        return R.layout.activity_tabmanager;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityTabmanagerBinding) mRootBinding;
        mPresenter = new TabManagerPresenter(this,this);
        mPresenter.start();
        setBackNavigationTip(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxBus2.getInstance().post(new TabEnent());
                finish();
            }
        });
        setBarTitle("频道管理");
        setRigTv("保存", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<ChannelBean> list = mAdapter.getChannelList();
                List<TabEntity.SavedBean> mTabList = new ArrayList<>();
                TabEntity.SavedBean mTabBean = null;
                //获取我的频道
                list.remove(0);
                for (ChannelBean bean : list){
                    if (bean.getName().equals(bean.getFistSaveName())||bean.getName().equals("无")){
                        break;
                    }else {
                        mTabBean = new TabEntity.SavedBean();
                        mTabBean.setId(bean.getId());
                        mTabBean.setName(bean.getName());
                        mTabBean.setType(bean.getSort());
                        mTabList.add(mTabBean);
                    }
                }
                TabEntity entity = new TabEntity();
                entity.setSaved(mTabList);
                mPresenter.saveTab(entity.getSaved());
            }
        });
//        setRigImg(R.drawable.ic_title_save, new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });
    }

    @Override
    protected void start() {

    }

    @Override
    public void refreshItemDecoration() {
        mBinding.recyclerView.invalidateItemDecorations();
    }

    @Override
    public void returnTabList(TabEntity entity) {
        mList = new ArrayList<>();
        GridLayoutManager manager = new GridLayoutManager(this, 4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return mList.get(position).getSpanSize();
            }
        });
        mBinding.recyclerView.setLayoutManager(manager);
        DefaultItemAnimator animator = new DefaultItemAnimator();
        animator.setMoveDuration(300);     //设置动画时间
        animator.setRemoveDuration(0);
        mBinding.recyclerView.setItemAnimator(animator);


        //我的频道
        ChannelBean title = new ChannelBean();
        title.setLayoutId(com.base.home.R.layout.adapter_title);
        title.setSpanSize(4);
        title.setSort(0);
        mList.add(title);

        //我的频道内容
        for (TabEntity.SavedBean bean : entity.getSaved()) {
            mList.add(new ChannelBean(bean.getId(),bean.getName(), 1, com.base.home.R.layout.adapter_channel, true,bean.getType(),entity.getSaved().size(),entity.getTree().size()>0?entity.getTree().get(0).getName():"无"));
        }

        for (TabEntity.TreeBean treeBean : entity.getTree()){
            ChannelBean tabBean = new ChannelBean();
            tabBean.setLayoutId(com.base.home.R.layout.adapter_tab);
            tabBean.setName(treeBean.getName());
            tabBean.setSpanSize(4);
            tabBean.setFistSaveName(entity.getTree().size()>0?entity.getTree().get(0).getName():"无");
            tabBean.setSaveSize(entity.getSaved().size());
            tabBean.setSort(1);
            mList.add(tabBean);

            for (TabEntity.SavedBean savedBean : treeBean.getTabs()){
                List<ChannelBean> recommendList = new ArrayList<>();
                recommendList.add(new ChannelBean(savedBean.getId(),savedBean.getName(), 1, com.base.home.R.layout.adapter_channel, true,savedBean.getType(),entity.getSaved().size(),entity.getTree().size()>0?entity.getTree().get(0).getName():"无"));
                mList.addAll(recommendList);
            }
        }

//        ChannelBean tabBean = new ChannelBean();
//        tabBean.setLayoutId(com.base.home.R.layout.adapter_tab);
//        tabBean.setName("更多频道");
//        tabBean.setSpanSize(4);
//        tabBean.setSort(1);
//        mList.add(tabBean);

//        List<ChannelBean> recommendList = new ArrayList<>();
//        for (TabEntity.TreeBean bean : entity.getTree()) {
//            recommendList.add(new ChannelBean(bean.getId(),bean.getName(), 1, com.base.home.R.layout.adapter_channel, true,bean.getType()));
//        }
//        mList.addAll(recommendList);


        for (int i=0;i<mList.size();i++){
            mList.get(i).setCurrentPosition(i);
        }
        mAdapter = new ChannelAdapter(this, mList);
        mAdapter.setFixSize(1);
        mAdapter.setSelectedSize(entity.getSaved().size());
        mAdapter.setRecommend(true);
        mAdapter.setOnItemRangeChangeListener(this);
        mBinding.recyclerView.setAdapter(mAdapter);
        WindowManager m = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        int spacing = (m.getDefaultDisplay().getWidth() - dip2px(this, 70) * 4) / 7;
        mBinding.recyclerView.addItemDecoration(new GridSpacingItemDecoration(4,spacing,true));
        ItemDragCallback callback=new ItemDragCallback(mAdapter,2);
        ItemTouchHelper helper=new ItemTouchHelper(callback);
        helper.attachToRecyclerView(mBinding.recyclerView);
    }

    @Override
    public void returnSaveSuccess(List<TabEntity.SavedBean> list) {
        TabEntity entity = new TabEntity();
        entity.setSaved(list);
        RxBus2.getInstance().post(entity);
        finish();
    }

    @Override
    public void onBackPressedSupport() {
        RxBus2.getInstance().post(new TabEnent());
        finish();
    }
}
